import { defineStore } from 'pinia'
import * as admin from '@/apis/admin'

let isFetched = false

export const useUserInfo = defineStore('useUserInfo', {
  state: () => {
    return {
      userinfo: <any>null,
      username: ''
    }
  },
  getters: {
    username(): string {
      return this.userinfo?.userid || ''
    }
  },
  actions: {
    async getUserInfo(must?: boolean) {
      if ((!isFetched || must) && !this.userinfo) {
        try {
          isFetched = true
          const data = (await admin.cookieLogin()).data
          if (data.code == 200) this.userinfo = data.data
        } catch (error) {
          //
        }
      }
      return this.userinfo
    }
  }
})
